// pages/shangpu_sale_next/shangpu_sale_next.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getpeitaourl: app.data.url + 'api/newapi/getPeiTao',
    addoutshangpuchusouurl: app.data.url + 'api/newapi/addout',
    addoutUrl: app.data.url + 'api/newapi/addout',
    changeouturl: app.data.url + 'api/newapi/changeout',
    shuju:'',
    address: '',    //商铺地址
    wuyefei: '',    //物业费
    shuifei: '',    // 水费
    dianfei: '',   // 电费
    title: '',    // 输入的标题
    miaosu: '',
    biaoqian: '',
    xuanzepeitao:[],
    peitao: [  //配套设施
      {
        id: 0,
        name: "客梯",
        checked: 'false'
      },
      {
        id: 1,
        name: "货梯",
        checked: 'false'
      },
      {
        id: 2,
        name: "空调",
        checked: 'false'
      },
      {
        id: 3,
        name: "停车位",
        checked: 'false'
      },
      {
        id: 4,
        name: "网络",
        checked: 'false'

      },
      {
        id: 5,
        name: "暖气",
        checked: 'false'

      },
      {
        id: 6,
        name: "扶梯",
        checked: 'false'

      },
      {
        id: 7,
        name: "上水",
        checked: 'false'

      },
      {
        id: 8,
        name: "下水",
        checked: 'false'

      },
      {
        id: 9,
        name: "天然气",
        checked: 'false'

      },
      {
        id: 10,
        name: "管煤",
        checked: 'false'

      },
      {
        id: 11,
        name: "排烟",
        checked: 'false',



      },
      {
        id: 12,
        name: "排污",
        checked: 'false'
      },
      {
        id: 13,
        name: "380v",
        checked: 'false'
      },
      {
        id: 14,
        name: "可明火",
        checked: 'false'
      },
      // {
      //   id: 15,
      //   name: "外摆区",
      //   checked: 'false'
      // }
    ],
    // 位置信息选择
    quyuxianshi: true,
    region: ['全部', '全部', '全部'],
    customItem: '全部',
    // 临街状态
    items: [
      { name: '1', value: '是' },
      { name: '0', value: '否', checked: 'true' },
    ],
kongzhi:1
  },

  /**
    * 添加标签
    */
  biaoqian1: function (e) {
    this.setData({ biaoqian1: e.detail.value });
    console.log(e.detail.value);
  },
  biaoqian2: function (e) {
    this.setData({ biaoqian2: e.detail.value });
    console.log(e.detail.value);
  },
  biaoqian3: function (e) {
    this.setData({ biaoqian3: e.detail.value });
    console.log(e.detail.value);
  },

  // bindRegionChange: function (e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     region: e.detail.value
  //   })
  // },


  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shu = e.detail.value;
    var shuju = this.data.shuju;
    shuju.sheng = shu[0];
    shuju.shi = shu[1];
    shuju.qu = shu[2];
    console.log(shuju);
    this.setData({shuju:shuju});
    this.setData({
      region: e.detail.value
    })
  },
  // 配套设施
  mypeitao: function (e) {
    var that = this;
    var peitao = that.data.peitao;
    var id = e.currentTarget.dataset.id;
    var newpeitao = [];
    ea.each(peitao,function(i,v){
      if(v.id==id){
        if(v.checked==true){
          v.checked = false;
        }else{
          v.checked = true;
        }
      }
      newpeitao.push(v);
    });
    that.setData({ peitao:newpeitao})
  },
  // 临街状态函数
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var shuju = this.data.shuju;
    shuju.near_jie = e.detail.value;
    console.log(shuju)
    this.setData({ shuju: shuju});
  },
  xiougai:function(){
    if (this.data.kongzhi == 0) {
      return;
    }
    this.setData({ kongzhi: 0 })
    var that = this;
    var shuju = that.data.shuju;
    var biaoqian = [];
    if (shuju.biaoqian1) {
      biaoqian.push(shuju.biaoqian1);
    }
    if (shuju.biaoqian2) {
      biaoqian.push(shuju.biaoqian2);
    }
    if (shuju.biaoqian3) {
      biaoqian.push(shuju.biaoqian3);
    }
    shuju.biaoqian = biaoqian;
    var easy_traffic = [];
    if (shuju.easy_traffic1) {
      easy_traffic.push(shuju.easy_traffic1);
    }
    if (shuju.easy_traffic2) {
      easy_traffic.push(shuju.easy_traffic2);
    }
    shuju.easy_traffic = easy_traffic;
    var peitaosesi = [];
    var peitao = that.data.peitao;
    ea.each(peitao, function (i, v) {
      if (v.checked == true) {
        peitaosesi.push(v.id);
      }
    })
    shuju.peitao = peitaosesi;
    var yan = ea.yanzhen(shuju, {
      imgs: '请填入图片',
      types: '请填入商铺类型',
      louceng: '请填入楼层',
      proportion: '请填入面积',
      width: '请填入面宽',
      height: '请填入层高',
      depth: '请填入进深',
      zhuangtai: '请填入经营状态',
      zujin: '请填入月租',
      fukuan_fangshi: '请填入付款方式',
      last_name: '请填入联系人姓',
      sex: '请填写性别',
      lxr_phone: '请填入联系手机',
      sheng: '请选择所在区域',
      shi: '请选择所在区域',
      qu: '请选择所在区域',
      title: '标题必填',
      dir: '请填写描述',
      regin:'请输入详细地址'
    });
    if (yan) {
      wx.showToast({
        title: yan,
        icon: 'none'
      })
      that.setData({ kongzi: 1 })
      return;
    }
    var img = shuju.img;
    var imgs = shuju.imgs;
    var delimg = [];
    var newimg = [];
    ea.each(imgs, function (i, v) {
      if (img.indexOf(v) == -1) {
        newimg.push(v);
      }
    })
    ea.each(img, function (i, v) {
      if (imgs.indexOf(v) == -1) {
        delimg.push(v);
      }
    })
    shuju.delimg = delimg;
    shuju.newimg = newimg;
    shuju.openid = wx.getStorageSync('openid');
    ea.upfile(newimg, function (v) {
      shuju.newimg = v;
      wx.request({
        url: that.data.changeouturl,
        data: shuju,
        method: 'post',
        success: function (res) {
          var data = res.data;
          if (data.code == 200) {
            wx.showToast({
              title: data.msg,
              icon: 'success'
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 2
              })
            }, 1000)
          } else {
            that.setData({kongzhi:1})
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
        }
      })
    })
  },
  // 提交函数
  confirmziliao: function () {
    if (this.data.kongzhi == 0) {
      return;
    }
    this.setData({ kongzhi: 0 })
    var that = this;
    // 获取配套设施的所有配套；
    var peitao = that.data.peitao;
    var shuju = that.data.shuju;
    var shu = ea.yanzhen(shuju,{
      sheng: '请选择所在区域',
      shi: '请选择所在区域',
      qu: '请选择所在区域',
      title: '标题必填',
      dir: '请填写描述',
      regin: '请输入详细地址'
    });
    if(shu){
      wx.showToast({
        title: shu,
        icon:'none'
      })
      that.setData({kongzhi:1})
      return;
    }
    var pei = [];
    ea.each(peitao,function(i,v){
      if(v.checked==true){
        pei.push(v.id);
      }
    })
    shuju.peitao = pei;
    shuju.type = 2;
    var biaoqian = [];
    if(shuju.biaoqian1){
      biaoqian.push(shuju.biaoqian1);
    }
    if (shuju.biaoqian2) {
      biaoqian.push(shuju.biaoqian2);
    }
    if (shuju.biaoqian3) {
      biaoqian.push(shuju.biaoqian3);
    }
    shuju.biaoqian = biaoqian;
    var easy_traffic = [];
    if (shuju.easy_traffic1){
      easy_traffic.push(shuju.easy_traffic1);
    }
    if (shuju.easy_traffic2) {
      easy_traffic.push(shuju.easy_traffic2);
    }
    shuju.easy_traffic = easy_traffic;
    shuju.out_type = 1;
    ea.upfile(shuju.imgs,function(v){
      shuju.imgs = v;
      var openid = wx.getStorageSync('openid');
      shuju.openid = openid;
      wx.request({
        url: that.data.addoutshangpuchusouurl,
        method:'post',
        data:shuju,
        success:function(res){
          var data = res.data;
          if(data.code==200){
            wx.showToast({
              title: '提交成功',
              icon: 'success'
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/layout/layout',
              })
            }, 1000);
          }else{
            that.setData({kongzhi:1})
            wx.showToast({
              title: data.msg,
              icon:'none'
            })
          }
        }
      })
    });
  },
  // 获取页面所有input框中的值
  inputWacth: function (e) {
    let item = e.currentTarget.dataset.model;
    var shuju = this.data.shuju;
    shuju[item] = e.detail.value;
    console.log(shuju);
    this.setData({ shuju: shuju});
    this.setData({
      [item]: e.detail.value
    });
    // console.log(this.data.address, this.data.wuyefei, this.data.shuifei, this.data.dianfei, this.data.title, this.data.miaosu)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shuju = wx.getStorageSync('shuju');
    var zpeitao = wx.getStorageSync('peitao');
    console.log(zpeitao);
    var peitao = [];
    ea.each(zpeitao, function (i, v) {
      peitao.push({ id: v.id, name: v.title, checked: false });
    })
    this.setData({ peitao: peitao });
    if (shuju.peitao) {
      var newpeitao = [];
      ea.each(peitao, function (i, v) {
        if (shuju.peitao.indexOf(v.id) == -1) {
          v.checked = false;
        } else {
          v.checked = true;
        }
        newpeitao.push(v);
      })
      this.setData({ peitao: newpeitao })
    }
    if (shuju.id) {
      var region = [];
      region.push(shuju.sheng);
      region.push(shuju.shi);
      region.push(shuju.qu);
      this.setData({ region: region });
      if (shuju.near_jie == 1) {
        var items = [
          { name: '1', value: '是', checked: true },
          { name: '0', value: '否', checked: false },
        ];
      } else {
        var items = [
          { name: '1', value: '是', checked: false },
          { name: '0', value: '否', checked: true },
        ];
      }
      this.setData({ items: items })
    }
    wx.setStorageSync('shuju', '');
    wx.setStorageSync('peitao', '');
    this.setData({ shuju: shuju });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})